package com.chaoyi.controller;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.model.Classification;
import com.chaoyi.model.KoreanVideo;
import com.chaoyi.service.ClassificationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @Auther: 卜加超
 * @Date: 2018/12/25 14:49
 * @Description:
 */
@Controller
@RequestMapping("/classification")
public class ClassificationController {
    private Logger log = LoggerFactory.getLogger(ClassificationController.class);

    @Resource
    private ClassificationService classificationService;
    @RequestMapping("/list")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ModelAndView mod = new ModelAndView("/classification/classificationList");
        return mod;
    }

    @RequestMapping("/classificationList")
    @ResponseBody
    public JSONObject videoList(int offset, int limit, Classification classification) {
        JSONObject json = new JSONObject();

        int count = classificationService.findCount(classification);

        List<Classification> list = new ArrayList<Classification>();
        if (count > 0) {
            list = classificationService.findList(offset, limit, classification);
        }
        json.put("rows", list);
        json.put("total", count);
        return json;
    }


    /**
     * 跳转到添加分类
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/add_plus")
    public ModelAndView add_plus(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("/classification/classification_add");
        return mv;
    }
    /**
     * 跳转到修改页面
     */
    @RequestMapping("/alertClassification")
    public ModelAndView alertVideo(HttpServletRequest request, HttpServletResponse response,int id) {

        ModelAndView mod = new ModelAndView("/classification/classification_update");
       Classification entity = classificationService.findClassificationOne(id);
       System.out.println(entity.toString());
        mod.addObject("obj",entity);
        return mod;
    }

    /**
     * 添加分类
     * @param entity
     * @return
     */
    @ResponseBody
    @RequestMapping("classificationAdd")
    public JSONObject addClassificationSave(Classification entity) {
        JSONObject json = new JSONObject();
        int number = classificationService.addClassificationSave(entity);
        if (number == 1) {
            json.put("missage", "success");
        } else {
            json.put("missage", "error");
        }
        return json;
    }

    /**
     * 保存修改
     */
    @ResponseBody
    @RequestMapping("edit")
    public JSONObject alertClassificationSave(HttpServletRequest request, HttpServletResponse response, Classification entity) {
        JSONObject json = new JSONObject();
        int number = classificationService.edit(entity);
        if (number == 1) {
            json.put("missage", "success");
        } else {
            json.put("missage", "error");
        }
        return json;
    }

    /**
     * 删除分类
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteClassification")
    public JSONObject deleteClassification(HttpServletRequest request, HttpServletResponse response, int id) {
        JSONObject json = new JSONObject();
        int number = classificationService.deleteClassification(id);
        if (number == 1) {
            json.put("missage", "success");
        } else {
            json.put("missage", "error");
        }
        return json;
    }

    /***
     * 查询视频种类信息, 形成select2 所支持的数据格式
     * @param entity
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/queryClassification")
    @ResponseBody
    public List<Map<String, Object>> queryCpInfoNotRole(Classification entity, HttpServletRequest request,
                                                        HttpServletResponse response) {
        List<Map<String, Object>> resultList = classificationService.getClassificationList(entity);

        log.info("分类有："+resultList.toString());
        return resultList;
    }

    @RequestMapping("/getClassification")
    public ModelAndView getClassification(){
        ModelAndView mv = new ModelAndView("/videoCollection/classification");
        return mv;
    }

}
